// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "equipmentName": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "装备名称",
    "label": "装备名称"
  },
  "imgFile": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "title": "装备图片",
    "label": "装备图片"
  },
  "equipmentParameter": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "装备参数",
    "label": "装备参数"
  },
  "useMethod": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "使用方法",
    "label": "使用方法"
  },
  "remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "备注",
    "label": "备注"
  },
  "storageState": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "在库状态",
    "label": "在库状态"
  },
  "equipmentStateInfo": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "建档阶段",
            "value": 0
          },
          {
            "text": "在库可用",
            "value": 1
          },
          {
            "text": "领用出库",
            "value": 2
          },
          {
            "text": "上报故障",
            "value": 3
          },
          {
            "text": "确认故障",
            "value": 4
          },
          {
            "text": "维修中",
            "value": 5
          },
          {
            "text": "已报废",
            "value": 6
          },
          {
            "text": "禁止使用",
            "value": 7
          }
        ]
      }
    ],
    "title": "装备状态信息",
    "label": "装备状态信息"
  },
  "purchaseTime": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "装备购入时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "装备购入时间"
  },
  "overInsuranceTime": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "装备过保时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "装备过保时间"
  }
}

const enumConverter = {
  "equipmentStateInfo_valuetotext": {
    "0": "建档阶段",
    "1": "在库可用",
    "2": "领用出库",
    "3": "上报故障",
    "4": "确认故障",
    "5": "维修中",
    "6": "已报废",
    "7": "禁止使用"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
